﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Xml;

namespace MM.Common.Base
{
    public static class Converts
    {
        // Methods
        public static DateTime ConverToDateTime(object obj, DateTime default_value)
        {
            if ((obj is DBNull) || (obj == null))
            {
                return default_value;
            }
            try
            {
                return Convert.ToDateTime(obj);
            }
            catch
            {
                return default_value;
            }
        }

        public static DateTime ConverToDateTime(DataRow dr, string col_name, DateTime default_value)
        {
            return ConverToDateTime(dr[col_name], default_value);
        }

        public static decimal ConverToDecimal(object obj, decimal default_value)
        {
            if (((obj is DBNull) || (obj == null)) || !obj.ToString().IsNumeric())
            {
                return default_value;
            }
            try
            {
                return Convert.ToDecimal(obj);
            }
            catch
            {
                return default_value;
            }
        }

        public static decimal ConverToDecimal(DataRow dr, string col_name, decimal default_value)
        {
            return ConverToDecimal(dr[col_name], default_value);
        }

        public static double ConverToDouble(object obj, double default_value)
        {
            if (((obj is DBNull) || (obj == null)) || !obj.ToString().IsNumeric())
            {
                return default_value;
            }
            try
            {
                return Convert.ToDouble(obj);
            }
            catch
            {
                return default_value;
            }
        }

        public static double ConverToDouble(DataRow dr, string col_name, double default_value)
        {
            return ConverToDouble(dr[col_name], default_value);
        }

        public static int ConverToInt(object obj, int default_value)
        {
            if (((obj is DBNull) || (obj == null)) || !obj.ToString().IsInt())
            {
                return default_value;
            }
            try
            {
                return Convert.ToInt32(obj);
            }
            catch
            {
                return default_value;
            }
        }

        public static int ConverToInt(DataRow dr, string col_name, int default_value)
        {
            return ConverToInt(dr[col_name], default_value);
        }

        public static long ConverToLong(object obj, long default_value)
        {
            if (((obj is DBNull) || (obj == null)) || !obj.ToString().IsInt())
            {
                return default_value;
            }
            try
            {
                return Convert.ToInt64(obj);
            }
            catch
            {
                return default_value;
            }
        }

        public static long ConverToLong(DataRow dr, string col_name, long default_value)
        {
            return ConverToLong(dr[col_name], default_value);
        }

        public static short ConverToShort(object obj, short default_value)
        {
            if (((obj is DBNull) || (obj == null)) || !obj.ToString().IsInt())
            {
                return default_value;
            }
            try
            {
                return Convert.ToInt16(obj);
            }
            catch
            {
                return default_value;
            }
        }

        public static short ConverToShort(DataRow dr, string col_name, short default_value)
        {
            return ConverToShort(dr[col_name], default_value);
        }

        public static string ConverToString(object obj, string default_value)
        {
            if ((obj is DBNull) || (obj == null))
            {
                return default_value;
            }
            try
            {
                return Convert.ToString(obj);
            }
            catch
            {
                return default_value;
            }
        }

        public static string ConverToString(DataRow dr, string col_name, string default_value)
        {
            return ConverToString(dr[col_name], default_value);
        }

        public static string DataSetToXML(DataSet xmlDS)
        {
            MemoryStream w = null;
            XmlTextWriter writer = null;
            string str;
            try
            {
                w = new MemoryStream();
                writer = new XmlTextWriter(w, Encoding.Unicode);
                xmlDS.WriteXml(writer);
                int length = (int)w.Length;
                byte[] buffer = new byte[length];
                w.Seek(0, SeekOrigin.Begin);
                w.Read(buffer, 0, length);
                UnicodeEncoding encoding = new UnicodeEncoding();
                str = encoding.GetString(buffer).Trim();
            }
            catch (Exception exception)
            {
                throw exception;
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                }
            }
            return str;
        }

        public static string DataTableToXml(DataTable dt)
        {
            if (dt != null)
            {
                MemoryStream w = null;
                XmlTextWriter writer = null;
                try
                {
                    w = new MemoryStream();
                    writer = new XmlTextWriter(w, Encoding.Unicode);
                    dt.WriteXml(writer);
                    int length = (int)w.Length;
                    byte[] buffer = new byte[length];
                    w.Seek(0, SeekOrigin.Begin);
                    w.Read(buffer, 0, length);
                    UnicodeEncoding encoding = new UnicodeEncoding();
                    return encoding.GetString(buffer).Trim();
                }
                catch (Exception exception)
                {
                    throw exception;
                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Close();
                        w.Close();
                        w.Dispose();
                    }
                }
            }
            return "";
        }

        public static string DataTableToXml(DataView dv)
        {
            return DataTableToXml(dv.Table);
        }

        public static string DataTableToXml(DataSet ds, int tableIndex)
        {
            if (tableIndex != -1)
            {
                return DataTableToXml(ds.Tables[tableIndex]);
            }
            return DataTableToXml(ds.Tables[0]);
        }

        public static bool DataTableToXmlFile(DataSet ds, string path, string xmlFilePath)
        {
            return DataTableToXmlFile(ds, -1, path, xmlFilePath);
        }

        public static bool DataTableToXmlFile(DataTable dt, string path, string xmlFilePath)
        {
            if ((dt != null) && !string.IsNullOrEmpty(xmlFilePath))
            {
                MemoryStream w = null;
                XmlTextWriter writer = null;
                try
                {
                    w = new MemoryStream();
                    writer = new XmlTextWriter(w, Encoding.Unicode);
                    dt.WriteXml(writer);
                    int length = (int)w.Length;
                    byte[] buffer = new byte[length];
                    w.Seek(0, SeekOrigin.Begin);
                    w.Read(buffer, 0, length);
                    UnicodeEncoding encoding = new UnicodeEncoding();
                    StreamWriter writer2 = new StreamWriter(path);
                    writer2.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                    writer2.WriteLine(encoding.GetString(buffer).Trim());
                    writer2.Close();
                    return true;
                }
                catch (Exception exception)
                {
                    throw exception;
                }
                finally
                {
                    if (writer != null)
                    {
                        writer.Close();
                        w.Close();
                        w.Dispose();
                    }
                }
            }
            return false;
        }

        public static bool DataTableToXmlFile(DataView dv, string path, string xmlFilePath)
        {
            return DataTableToXmlFile(dv.Table, path, xmlFilePath);
        }

        public static bool DataTableToXmlFile(DataSet ds, int tableIndex, string path, string xmlFilePath)
        {
            if (tableIndex != -1)
            {
                return DataTableToXmlFile(ds.Tables[tableIndex], path, xmlFilePath);
            }
            return DataTableToXmlFile(ds.Tables[0], path, xmlFilePath);
        }

        public static object SetDbDateTime(object obj)
        {
            if (((obj != null) && (obj != DBNull.Value)) && !(obj.ToString() == string.Empty))
            {
                return Convert.ToDateTime(obj);
            }
            return DBNull.Value;
        }

        public static object SetDbDecimal(object obj)
        {
            if (((obj != null) && (obj != DBNull.Value)) && !(obj.ToString() == string.Empty))
            {
                return Convert.ToDecimal(obj);
            }
            return DBNull.Value;
        }

        public static object SetDbInt(object obj)
        {
            if (((obj != null) && (obj != DBNull.Value)) && !(obj.ToString() == string.Empty))
            {
                return Convert.ToInt16(obj);
            }
            return DBNull.Value;
        }

        public static object SetDbLong(object obj)
        {
            if (((obj != null) && (obj != DBNull.Value)) && !(obj.ToString() == string.Empty))
            {
                return Convert.ToInt32(obj);
            }
            return DBNull.Value;
        }

        public static object SetDbString(object obj)
        {
            if (((obj != null) && (obj != DBNull.Value)) && !(obj.ToString() == string.Empty))
            {
                return obj.ToString();
            }
            return DBNull.Value;
        }

        public static DataSet XmlFileToDataSet(string path, string xmlFilePath)
        {
            if (!string.IsNullOrEmpty(xmlFilePath))
            {
                StringReader input = null;
                XmlTextReader reader = null;
                try
                {
                    XmlDocument document = new XmlDocument();
                    document.Load(path);
                    DataSet set = new DataSet();
                    input = new StringReader(document.InnerXml);
                    reader = new XmlTextReader(input);
                    set.ReadXml(reader);
                    return set;
                }
                catch (Exception exception)
                {
                    throw exception;
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                        input.Close();
                        input.Dispose();
                    }
                }
            }
            return null;
        }

        public static DataTable XmlFileToDataTable(string path, string xmlFilePath)
        {
            return XmlFileToDataSet(path, xmlFilePath).Tables[0];
        }

        public static DataTable XmlFileToDataTable(string path, string xmlFilePath, int tableIndex)
        {
            return XmlFileToDataSet(path, xmlFilePath).Tables[tableIndex];
        }

        public static DataSet XmlToDataSet(string xmlStr)
        {
            if (!string.IsNullOrEmpty(xmlStr))
            {
                StringReader input = null;
                XmlTextReader reader = null;
                try
                {
                    DataSet set = new DataSet();
                    input = new StringReader(xmlStr);
                    reader = new XmlTextReader(input);
                    set.ReadXml(reader);
                    return set;
                }
                catch (Exception exception)
                {
                    throw exception;
                }
                finally
                {
                    if (reader != null)
                    {
                        reader.Close();
                        input.Close();
                        input.Dispose();
                    }
                }
            }
            return null;
        }

        public static DataTable XmlToDatatTable(string xmlStr)
        {
            return XmlToDataSet(xmlStr).Tables[0];
        }

        public static DataTable XmlToDatatTable(string xmlStr, int tableIndex)
        {
            return XmlToDataSet(xmlStr).Tables[tableIndex];
        }
    }
}
